import { computed, toRaw } from 'vue';
import { unref } from 'vue';
import { uniq } from 'lodash-es';
import { getAllParentPath } from '/@/router/helper/menuHelper';
import { useTimeoutFn } from '/@/hooks/core/useTimeout';
import { useDebounceFn } from '@vueuse/core';
export function useOpenKeys(menuState, menus, accordion, mixSider, collapse) {
    const debounceSetOpenKeys = useDebounceFn(setOpenKeys, 50);
    async function setOpenKeys(path) {
        const native = !mixSider.value;
        const menuList = toRaw(menus.value);
        useTimeoutFn(() => {
            if (menuList?.length === 0) {
                menuState.activeSubMenuNames = [];
                menuState.openNames = [];
                return;
            }
            const keys = getAllParentPath(menuList, path);
            if (!unref(accordion)) {
                menuState.openNames = uniq([...menuState.openNames, ...keys]);
            }
            else {
                menuState.openNames = keys;
            }
            menuState.activeSubMenuNames = menuState.openNames;
        }, 30, native);
    }
    const getOpenKeys = computed(() => {
        return unref(collapse) ? [] : menuState.openNames;
    });
    return { setOpenKeys: debounceSetOpenKeys, getOpenKeys };
}
